package com.sdkj.action.base;

import java.lang.reflect.ParameterizedType;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sdkj.Entity.BaseEntity;
import com.sdkj.Entity.admin.AdminUser;
import com.sdkj.constant.WebConst;
import com.sdkj.log.OperationLog;
import com.sdkj.service.base.IBaseService;
import com.sdkj.service.log.IOperationLogService;
/**
 * action 基类
 * @author sdkj
 *
 * @param <T> PO
 * @param <K> service 接口
 */
@Scope("prototype")
public abstract class BaseAction<T extends BaseEntity,K extends IBaseService<T>> extends ActionSupport implements ModelDriven<T>,WebConst{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected String id;
	
	protected String ids; 
	
	protected T model;
	protected K service;
	
	//参数查询
//	protected String hql;
//	protected Map<String,Object>params=new HashMap<String,Object>();
	
	protected int rows=10;//每页显示的记录数
	protected int page=1;//当前页 
	protected String order;// 'asc' or 'desc'
	protected String sort;// 按照哪种属性排序
	
	protected String fr;
	
	@Autowired
	public void setService(K service) {
		this.service = service;
	}
	
	//================ 实现 ModelDriven 的支持============//
	
	public BaseAction(){
		try {
			ParameterizedType pt=(ParameterizedType)this.getClass().getGenericSuperclass();
			@SuppressWarnings("unchecked")
			Class<T>clazz=(Class<T>)pt.getActualTypeArguments()[0];
			model=clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	protected HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	
	protected String getSimpleClazz() {
		return (String) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getClass().getSimpleName();
	}
	public String getRemoteAddrIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");  
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }
        if (ip.indexOf(",") > 0) {
       	 ip = ip.split(",")[0];
        }
        return ip;  
	}
	
	@Resource
	protected IOperationLogService operationLogService;
	
	public OperationLog getOperationLogBean(String opname,String... ignoField){
		OperationLog ol=new OperationLog();
		AdminUser sessionUser=(AdminUser) getSession().getAttribute("curuser");
		ol.setIp(getRemoteAddrIp(getRequest()));
		ol.setOpdevice("web");
		ol.setOperation(opname); 
		JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
		  jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略
		  jsonConfig.setExcludes(ignoField);
		ol.setParams(JSONObject.fromObject(getRequest().getParameterMap(),jsonConfig).toString());
		ol.setUrl(getRequest().getRequestURI());
		ol.setUid(sessionUser.getUuid());
		return ol;
	}

	//=======================  CRUD   ===========================//
	
	
	//================ 实现 Service 实例 的支持============//
//	@Resource
//	protected ITestService test;
//	
//	@Resource
//	protected ISysLogService logService;
//	
//	@Resource
//	protected IUserService userService;
//	
//	@Resource
//	protected IPermissionUserService permissionUserService;
//	
//	@Resource
//	protected DepartmentService departmentService;
//	
//	@Resource
//	protected RoleService roleService;
//	
//	@Resource
//	protected UserService userService;
//	
//	@Resource
//	protected PrivilegeService privilegeService;
	
	
	//=================== getter and  setter ===========================//
	public T getModel() {
		return model;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFr() {
		return fr;
	}

	public void setFr(String fr) {
		this.fr = fr;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
}
